package com.bawei.xmppdemo

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import androidx.databinding.DataBindingUtil
import androidx.viewbinding.ViewBinding
import com.bawei.xmppdemo.databinding.ActivityMainBinding
import com.baweigame.xmpplibrary.XmppManager
import com.baweigame.xmpplibrary.contract.StateListener
import org.jivesoftware.smack.MessageListener
import org.jivesoftware.smack.packet.Message
import org.jivesoftware.smackx.muc.MultiUserChat
import org.jxmpp.jid.EntityFullJid

class MainActivity : AppCompatActivity() {
    private lateinit var activityMainBinding:ActivityMainBinding
    lateinit var chat: MultiUserChat
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        activityMainBinding = DataBindingUtil.setContentView(this,R.layout.activity_main)
        //登陆
        activityMainBinding.btLogin.setOnClickListener {
            val username = activityMainBinding.etUsername.text.toString()
            val password = activityMainBinding.etPassword.text.toString()
            XmppManager.getInstance().xmppUserManager.login(username,password)
            Log.d("ytx", "登陆成功: ")
        }

        //注册
        activityMainBinding.btRegister.setOnClickListener {
            val username = activityMainBinding.etUsername.text.toString()
            val password = activityMainBinding.etPassword.text.toString()
            XmppManager.getInstance().xmppUserManager.createAccount(username,password)
            Log.d("ytx", "登陆失败: ")
        }
        //创建聊天室:主播
        activityMainBinding.btCreateRoom.setOnClickListener {
            if( XmppManager.getInstance().xmppUserManager.isLogin){
                chat= XmppManager.getInstance().xmppChatRoomManager.createRoom("国大军的值哦见", "")
                Log.d("ytx", "创建聊天室的名字: "+chat.toString())
                if(chat != null){
                    //接受消息：实时接受：注册监听
                    initRecevie()
                }


            }
        }
        //加入聊天室：观众
        activityMainBinding.btJoinRoom.setOnClickListener {
            if( XmppManager.getInstance().xmppUserManager.isLogin){
                val roomName = activityMainBinding.etRoom.text.toString()//聊天室的名称
                val username = activityMainBinding.etUsername.text.toString() //当前登陆的用户名
                chat =  XmppManager.getInstance().xmppChatRoomManager.joinMultiUserChat(username,roomName)
                if(chat.isJoined){
                    initRecevie()
                }
            }
        }
        //发送消息
        activityMainBinding.btSendMessage.setOnClickListener {
            if( XmppManager.getInstance().xmppUserManager.isLogin){
                val message = activityMainBinding.etMessage.text.toString()
                XmppManager.getInstance().xmppChatRoomManager.sendMessasge(chat,message)

            }
        }





    }

    private fun initRecevie() {
        if( XmppManager.getInstance().xmppUserManager.isLogin){
            //状态监听
            XmppManager.getInstance().xmppChatRoomManager.registerStateListener(chat,object :StateListener(){
                override fun joined(participant: EntityFullJid?) {
                    super.joined(participant)
                    Log.d("ytx", "进来了: "+participant)
                }

                override fun left(participant: EntityFullJid?) {
                    super.left(participant)
                    Log.d("ytx", "离开了: "+participant)
                }
            })
            //消息监听
            XmppManager.getInstance().xmppChatRoomManager.registerMsgListener(chat,object: MessageListener{
                override fun processMessage(message: Message?) {
                    Log.d("ytx", "接受消息: "+message.toString())
                }

            })



        }
    }


}